<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="referrer" content="no-referrer" />
    <title>模型对象</title>
    <style>
        body {
            position: absolute;
            margin: 0;
            width: 100%;
            height: 100%;
        }

        #mapContainer {
            left: 0px;
            top: 0px;
            width: 100%;
            height: 100%;
            float: left;
        }
    </style>
</head>

<body>

    <div id="mapContainer" style="position: relative;">
    </div>

</body>
<script src=""></script>
<script src='/GiswayEngine.js'></script>
<script type="text/javascript">
    var model;
    window.app = new GE.App('mapContainer', {
        position: [122.0013976037733, 40.71105917089209, 1000],
        lookAt: [122.00151626214978, 40.71106690828059, 0],
        cameraScrollSpeed: 15,
        minZoom: 3,
        maxZoom: 21,
        ambientLightIntensity: 5,
        ambientLightType: 'hemiSphere',
        useCarpet: true,
        maxSlopeAngle: 50, //地图最大倾斜角度
        background: "Fog",
        useDefaultTexture: true, //是否使用瓦片默认纹理
        extraStep: 1, //额外加载瓦片的步长
        // isOpenEyeIntersect: true, //是否开启相机与场景中其他物体的相交检测功能
        baseLayer: [
            {
                url: ' https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
                readableUrl: true,
                tileType: 'image',
            },
        ]
    });

    // loadModel('http://localhost:9000/examples/resources/gltf/ZHZG_CJ_A30A31_ZGX_01(1).glb', 'glb');
    // loadModel('http://localhost:9000/examples/resources/gltf/NeilArmstrong-宇航员.glb', 'glb');
    // loadModel('http://localhost:9000/examples/resources/3dtiles/test/3dtiles/model/JFKJ0313_SM_DaoLu01.fbx', 'fbx');
    // loadModel('http://localhost:9000/examples/resources/fbx/dianchi3.FBX', 'fbx');
    // loadModel('http://localhost:9000/examples/resources/fbx/kuerle.fbx', 'fbx');
    // loadModel('http://localhost:9000/examples/resources/fbx/电池测试模型/模型/XDCAQYWZNGLXT.FBX', 'fbx');
    loadModel('http://localhost:9000/examples/resources/fbx/face24.fbx', 'fbx');
    // loadModel('http://localhost:9000/examples/resources/obj/电池柜/111.obj', 'obj', 'http://localhost:9000/examples/resources/obj/电池柜/111.mtl');

    // btn();
    app.openStats();
    app.on('Click', function (e) {
        console.log(e)
    });
    function loadModel(url, type, extraUrl) {
        model = new GE.OverLay({
            type: type,
            url: url,
            // dracoPluginUrl: 'http://localhost:9000/examples/resources/plugin/draco/',
            mtlUrl: extraUrl,
            // url: 'http://localhost:9000/examples/resources/fbx/ceshi1.FBX',
            // url:'http://192.168.2.43:9000/examples/resources/fbx/box.gltf',
            position: [122.00151626214978, 40.71106690828059, 100.2],
            // changeEmissive: true,
            // gltfUpAxis: 'Z',
            gltfUpAxis: 'Y',
            scale: [1, 1, 1],
            canNotSelect: true,
            // rotation: [90, 0, 0],
            success: function (fbxParent) {
                setTimeout(btn, 300);
                // return;
                // setClickEvent(fbxParent);

                traverseMeshes(model, function (mesh) {
                    if (mesh.material.length > 1) {
                        mesh.material.forEach(element => {
                            // setEmmisive(element);
                            element.side = 2;
                            if (element.opacity != 0) {
                                element.transparent = true;
                                element.needsUpdate = true;
                            }

                        });
                    } else {
                        // setEmmisive(mesh.material);
                        mesh.material.side = 2;
                        if (mesh.material.opacity != 0) {
                            mesh.material.transparent = true;
                            mesh.material.needsUpdate = true;
                        }
                    }
                });
            }
        })
        app.add(model);
        // window.app.camera.addIntersectObjWithEye(model); //该模型和相机做相交检测，避免穿透

        window.model = model;
    }

    function traverseMeshes(mesh, callback) {
        // 对当前mesh执行回调
        if (mesh.type == 'Mesh') {
            callback(mesh);
        }
        // 遍历子节点
        mesh.children.forEach(child => {
            if (child.isObject3D) {
                traverseMeshes(child, callback);
            }
        });
    }

    function setEmmisive(material) {
        if (!material.emissive) {
            return;
        }
        material.emissive.set(1, 1, 1);
        material.emissiveIntensity = 1;
        material.emissiveMap = material.map;
    }

    function setClickEvent(fbxParent) {
        let meshArray = fbxParent.children[0].children;
        meshArray.forEach(mesh => {
            mesh.setClick = function (e) {
                e.object.material.opacity = 1;
                e.object.material.transparent = false;

                meshArray.forEach(element => {
                    if(e.object != element){
                        element.material.opacity = 0.1;
                        element.material.transparent = true;
                    }
                });
            }
        });
    }

    function btn() {
        let button = new GE.Button('模型显隐', function () {
            model.visible = !model.visible;
        });
        app.ui.add(button);
        window.button = button;

        let button2 = new GE.Button('模型右移10米', function () {
            model.position.x += 10;
        });
        app.ui.add(button2);

        let button3 = new GE.Button('获取模型详细信息', function () {
            let info = model.getModelInfo();
            alert(JSON.stringify(info));
            console.log(info);
        });
        app.ui.add(button3);


    }

    // addPOI(); //添加poi

    var poi;
    function addPOI() {

        poi = new GE.Poi({
            url: ' http://localhost:9000/examples/resources/images/warn.png',
            // url: 'http://192.168.2.100:8702/resources/1b8b2fcb26b020120aebfac57779e06a/images/tower.png', //模型图片路径
            position: new GE.Point(121.99974634043484, 40.71228650624186, 0), //POI位置，不传就是0,0,0原点
            name: 'Hello World', //'POI的名字', //显示的文字名称
            strokeStyle: '#000000', //文字描边颜色
            strokeWidth: 15, //文字描边宽度
            size: 1000, //文字大小
            isBold: true, //是否加粗
            baseImageSize: 1200, //图片大小（长宽一致，都是这个值）
            showText: true,//是否显示文字
            textStyle: '#DC143C',//字体颜色
            // textstyle: '#00FF0000',//字体颜色
            textfont: 'sans-serif',//字体格式 字体的支持情况跟操作系统和浏览器环境有关，sans-serif是默认字体都支持，其余字体不保证正常显示
            center: [0.5, 0.0], //POI的锚点,用于控制POI位置的偏移，默认是[0.5, 0.5]居中，如果是[0, 0]则在左下角显示，[1, 1]则在右上角显示
        })
        app.add(poi);

    }

</script>

</html>